package com.dmz.wxcp.redis;

import com.alibaba.fastjson.JSONObject;
import com.dmz.wxcp.wecom.common.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//一些redis工具的封装
@Component
public class RedisUtils {

//    要注意在静态类中使用autowired
    @Autowired
    private RedisService redisService;

    private static RedisUtils commonUtil;
    @PostConstruct
    public void init() {
        commonUtil = this;
    }

    public static <T> T RedisToObject(String jsonString,Class<T> clazz){
        JSONObject homeJson = JSONObject.parseObject(jsonString);

        return JSONObject.toJavaObject(homeJson,clazz);
    }

//    从redis中获取List集合
    public static <T> List<T> RedisToObjectList(String jsonString,Class<T> clazz){
        List<T> result = JSONObject.parseArray(jsonString,clazz);
        return result;
    }

    public static Map<String,Object> RedisToMap(String jsonString){
        JSONObject homeJson = JSONObject.parseObject(jsonString);
        Map<String,Object> resultMap=new HashMap<>();
        for (Map.Entry<String, Object> entry : homeJson.entrySet()) {
            resultMap.put(entry.getKey(), entry.getValue());
        }
        return resultMap;
    }




}
